cap log close

global path = "****"

local name= "age_cats_10yrs"
local date = "033020"
global logname = "$path/`name'_`date'.log"

clear
set more off
set matsize 11000
cd "$path"
log using "$logname", replace

use "$path/Data/penalty_sample.dta", clear

replace age_2017=0 if age_2017<0

*10 year age categories
egen age_cats_10yr = cut(age_2017), at(0,5,15,25,35,45,55,65,1000000)
tabulate age_cats_10yr, generate(age_cats_10yr_)



cap erase "$path/cov1718_age10_`date'.ster"
cap erase "$path/iv1718_age10_`date'.ster"

estimates clear
eststo clear

*loop over age bins for given age bin size
foreach age of varlist age_cats_10yr_*{

*months of coverage 1718 
reg covered1718 treatment if `age' == 1 , cl(tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)& treatment==0
estadd scalar ymean=r(mean)
estimates save "$path/cov1718_age`c'_`date'.ster", append

*IV
ivregress 2sls dead1718 (covered1718 = treatment) if `age' == 1  , cl(tin) 
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)&treatment==0
estadd scalar ymean=r(mean)*100
estimates save "$path/iv1718_age`c'_`date'.ster", append
}


log close

